
#include "pt_ipc.h"


#ifdef PT_BK7258
static meida_ipc_t meter_ipc = NULL;
#endif

static int media_ipc_lvgl_callback(uint8_t *data, uint32_t size, void *param)
{
    pt_ipc_msg_t *msg = NULL;

#ifdef PT_BK7258
    msg = (pt_ipc_msg_t *)data;
    if(!msg){
        return -1;
    }

    switch(msg->type){
    case IPC_MSG_REBOOT:
    {
        #if (CONFIG_SYS_CPU0)
        pt_reboot();
        #endif
        break;
    }     
    case IPC_MSG_DEVLIST:
    {
        pt_ipc_devlist_parse(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_SCENE:
    {
        #if (CONFIG_SYS_CPU1)
        dev_scene_parse(msg->buff,msg->len);
        #endif
        break;
    }
    case IPC_MSG_DWLED_ONOFF:
    {
        pt_gener_dwled_onoff_req(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_SWLED_ONOFF:
    {
        pt_gener_swled_onoff_req(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_MC_CTL:
    {
        pt_gener_dev_mc_req(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_OC_CTL:
    {
        pt_gener_dev_oc_req(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_JC_CTL:
    {
        pt_gener_dev_jc_req(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_TOKEN_STAT:
    {
        uint8_t token_stat = msg->buff[0];
        os_printf("ipc got token stat:%d-----\n",token_stat);
        pt_accredit_token_set(token_stat);
    }
    break;
    case IPC_MSG_NET_STAT:
    {
        uint8_t flag = msg->buff[0];
        os_printf("ipc got net et stat:%d-----\n",flag);
        pt_network_set(flag);
        break;
    }
    case IPC_MSG_RECNNT_STAT:
    {
        uint8_t flag = msg->buff[0];
        os_printf("ipc got reconnect flag:%d-----\n",flag);
        pt_recnnt_flag_set(flag);
        break;
    }
    case IPC_MSG_UNBIND_STAT:
    {
        uint8_t flag = msg->buff[0];
        os_printf("ipc got unbind flag:%d-----\n",flag);
        pt_unbind_flag_set(flag);
        break;
    }
    case IPC_MSG_SCR_OFF_TIME:
    {
        uint8_t flag = msg->buff[0];
        os_printf("ipc got scr_off_time %d-----\n",flag);
        root_scr_off_timer_interval_set(flag);
        break;
    }
    case KH_DEV_STATE_GET:
    {
        os_printf("ipc got kh_dev_state devid %s-----\n",msg->buff);

        break;
    }
    case KH_DEV_CTRL:
    {
        break;
    } 
    case KH_DEV_PURI_CTRL:
    {
        debug_d("send puri cmd\n");
        pt_gener_dev_puri_req(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_SCENE_EXEC:
    {
        debug_d("send scene cmd\n");
        pt_gener_scene_cmd_send(msg->buff,msg->len);
        break;
    }
    case IPC_MSG_SSE_TO_UI:
    {
        debug_d("send data to ui\n");
        #if (CONFIG_SYS_CPU1)
        pt_gener_sse_data_parse(msg->buff,msg->len);
        #endif
        break;
    }
    default:break;
    }
#endif

    return 0;
}

int pt_ipc_init(void)
{
    int ret = -1;

#ifdef PT_BK7258
	media_ipc_chan_cfg_t cfg = {0};
	cfg.cb    = media_ipc_lvgl_callback;
	cfg.name  = "app";
#if CONFIG_SYS_CPU0
    cfg.param = "cpu0";
#endif
#if CONFIG_SYS_CPU1
    cfg.param = "cpu1";
#endif	
    ret = media_ipc_channel_open(&meter_ipc, &cfg);
	if(ret != BK_OK){
		os_printf("open ipc failed\n");
	}
#endif

    return ret;
}

int pt_ipc_send_msg(int msg_type,char* msg_buff,int msg_len)
{
    int          ipc_msg_size = 0;
    pt_ipc_msg_t* ipc_msg = NULL;

    if((!msg_buff)||(!msg_len)){
        return -1;
    }

#ifdef PT_BK7258
    if(!meter_ipc){
        debug_d("ipc error\n");
        return -1;
    }
#endif


    ipc_msg = (pt_ipc_msg_t*)psram_malloc(sizeof(pt_ipc_msg_t));
    if(!ipc_msg){
        debug_e("m_error\n");
        return -1;
    }

    debug_d("\n");

    ipc_msg->buff = psram_malloc(msg_len);
    if(!ipc_msg->buff){
        debug_e("m_error\n"); 
        if(ipc_msg){
            psram_free(ipc_msg);
            ipc_msg = NULL;
        } 
        return -1;     
    }

    memcpy(ipc_msg->buff,msg_buff,msg_len);

    debug_d("\n");

    ipc_msg->type = msg_type;
    ipc_msg->len  = msg_len;
    ipc_msg_size  = sizeof(pt_ipc_msg_t) + msg_len;

#ifdef PT_BK7258
	bk_err_t ret;
	ret = media_ipc_send(&meter_ipc, (void*)ipc_msg,ipc_msg_size,MIPC_CHAN_SEND_FLAG_SYNC);
	if (ret != BK_OK){
		os_printf("media_ipc_send failed\n");
	}
#endif

    if(ipc_msg->buff){
        psram_free(ipc_msg->buff);
        ipc_msg->buff = NULL;
    }

    if(ipc_msg){
        psram_free(ipc_msg);
        ipc_msg = NULL;
    }

    debug_d("ipc send finish\n");

    return 0;
}
